package game;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class Respawn 
{
	static LinkedList<BaseEnemy> enemie;
	static LinkedList<Ainteligence> ia;
	float tempo = 1000;
	float tempoInstacia = 1;
	int maximo;
	int addedEnemies=0;
	
	public static LinkedList<BaseEnemy> getEnemie() {
		return enemie;
	}
	
	public Respawn(Point2D posicao, int maximo, float time)
	{
		this.maximo = maximo;
		tempoInstacia = time;
		enemie = new LinkedList<BaseEnemy>();
		ia = new LinkedList<Ainteligence>();	
	}
	public void Update(BaseEnemy e, Player player, Rectangle[] mapa, float velocityEnemie)
	{

		tempo += Time.getInstance().getLoopTime();
				
		if(tempo > tempoInstacia && enemie.size() < maximo)
		{
			addedEnemies++;
			enemie.addLast(e);
			ia.addLast(new Ainteligence(enemie.getLast(), player,mapa, velocityEnemie));
			tempo = 0;
		}
		
		for(int i = 0; i < enemie.size(); i++)
		{
			if(CapacitorManager.getInstance().capacitor1 != null)
				if(CapacitorManager.getInstance().capacitor1.coletado != true)					
					if(enemie.get(i).getRectangle().intersects(CapacitorManager.getInstance().capacitor1.getRectangle()))
					{
						Explosao.IniciarExplosaoBlack(CapacitorManager.getInstance().capacitor1.getRectangle(), 0.02f);
						CapacitorManager.getInstance().capacitor1 = null;
						
					}
			
			if(CapacitorManager.getInstance().capacitor2 != null)
				if(CapacitorManager.getInstance().capacitor2.coletado != true)
					if(enemie.get(i).getRectangle().intersects(CapacitorManager.getInstance().capacitor2.getRectangle()))
					{
						Explosao.IniciarExplosaoBlack(CapacitorManager.getInstance().capacitor2.getRectangle(), 0.02f);
						CapacitorManager.getInstance().capacitor2 = null; 
					}
			
			try{
				ia.get(i).Update();
				enemie.get(i).Update(player);
				enemie.set(i, (BaseEnemy) ia.get(i).returnEnemie());
			}
			catch(Exception fg){}
		}
		
		for(int i = 0; i < enemie.size(); i++)
		{
			
			if(enemie.get(i).isDead == true)
			{
				BaseLevel.addInimigoMorto(new Point((int)enemie.get(i).getPosition().getX(), (int)enemie.get(i).getPosition().getY()));
				enemie.remove(i);
				ia.remove(i);
				
			}
		}
	}

	public void Draw(Graphics2D g2d)
	{
		
		for(int i = 0; i < ia.size();i++)
		{
			enemie.get(i).draw(g2d);
		}
	}
}
